const { initAccessToken } = require('../libs/util');
const { wConfig } = require('../config');
const chalk = require('chalk');
module.exports = async (ctx, next) => {
    validateToken();
    /* let apiAddr = (/\/(?<url>\w+)\?{0,1}/).exec(ctx.request.url).groups.url;
    let urlList = ['getBaseInfo', 'getSign', 'setUserInfo', 'wx_login'];
    if (urlList.includes(apiAddr)) {
        let result = global.cache.get('wechatToken');
    } */
    await next();
}

function validateToken() {
    try {
        let result = global.cache.get('wechatToken');
        let now = Date.now();
        if (now > result.expires_in) {
            initAccessToken(wConfig);
        }
        else {
            console.log(chalk.cyan('--------合法token-------'));

        }
    } catch (error) {
        initAccessToken(wConfig);
    }
}